package com.eco.fanliapp.taobao;


import com.eco.fanliapp.utils.DateUtils;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkScInvitecodeGetRequest;
import com.taobao.api.request.TbkScMaterialOptionalRequest;
import com.taobao.api.request.TbkScPublisherInfoSaveRequest;
import com.taobao.api.response.TbkScInvitecodeGetResponse;
import com.taobao.api.response.TbkScMaterialOptionalResponse;
import com.taobao.api.response.TbkScPublisherInfoSaveResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Service
public class SearchController {
   


	/**
	 * 超级搜索
	 * @param session
	 * @param keyword
	 * @param adzoneId
	 * @param siteId
	 * @param sort
	 * @param pageSize
	 * @param pageNo
	 * @param hasCoupon
	 * @return
	 */
    @RequestMapping(value = "/taobao/item/optional", method = RequestMethod.POST)
    public TbkScMaterialOptionalResponse orderDetails(String session, String keyword, Long adzoneId, Long siteId, String sort, Long pageSize, Long
            pageNo, @RequestParam(required = false,defaultValue = "false") Boolean hasCoupon) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkScMaterialOptionalRequest req = new TbkScMaterialOptionalRequest();
        req.setPageSize(pageSize);
        req.setPageNo(pageNo);
        req.setSort(sort);
        req.setHasCoupon(hasCoupon);
        req.setQ(keyword);
        req.setAdzoneId(adzoneId);
        req.setSiteId(siteId);
        try {
            TbkScMaterialOptionalResponse rsp = client.execute(req, session);
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 渠道备案
     * @param inviterCode
     * @param session
     * @param note
     * @return
     */
    @RequestMapping(value = "/taobao/tbk/scPublisherInfo", method = RequestMethod.POST)
    public TbkScPublisherInfoSaveResponse tbkScPublisherInfo(String inviterCode, String session, String note) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkScPublisherInfoSaveRequest req = new TbkScPublisherInfoSaveRequest();
        req.setInviterCode(inviterCode);
        req.setInfoType(1L);
        req.setNote(note);
        try {
            TbkScPublisherInfoSaveResponse rsp = client.execute(req, session);
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return  null;
    }
    
    /**
     * 获取渠道邀请码
     * @param session
     * @return
     */
    @RequestMapping(value = "/taobao/tbk/cInvitecode", method = RequestMethod.POST)
    public TbkScInvitecodeGetResponse tbkScInvitecode(String session) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkScInvitecodeGetRequest req = new TbkScInvitecodeGetRequest();
        req.setRelationApp("common");
        req.setCodeType(1L);//1 - 渠道邀请
//        req.setCodeType(3L);//3 -会员邀请
        try {
            TbkScInvitecodeGetResponse rsp = client.execute(req, session);
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 获取渠道邀请码
     * @param session
     * @return
     */
    @RequestMapping(value = "/taobao/tbk/mInvitecode", method = RequestMethod.POST)
    public TbkScInvitecodeGetResponse tbkMeInvitecode(String session) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkScInvitecodeGetRequest req = new TbkScInvitecodeGetRequest();
        req.setRelationApp("common");
//        req.setCodeType(1L);//1 - 渠道邀请
        req.setCodeType(3L);//3 -会员邀请
        try {
            TbkScInvitecodeGetResponse rsp = client.execute(req, session);
            return rsp;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return  null;
    }
    
    public static void main(String[] args) {
//       TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
//        TbkScPublisherInfoSaveRequest req = new TbkScPublisherInfoSaveRequest();
//        req.setInviterCode("PFCBDD");
//        req.setInfoType(1L);
//        try {
//            TbkScPublisherInfoSaveResponse rsp = client.execute(req, "7000010134235629aa5b44583ad761624613381df5c3be084fa6151a718b1120ee88e25685884107");
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }



    }
}
